package com.tanhua.dubbo.mongo.api;


import com.tanhua.dubbo.db.api.PeachBlossomUserApi;
import com.tanhua.model.mongo.PeachBlossomUser;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

@DubboService
public class PeachBlossomUserApiImpl implements PeachBlossomUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    //将获取者的当日数据存入表中
    public void savePeachBlossomUser(PeachBlossomUser peachBlossomUser) {
        mongoTemplate.save(peachBlossomUser);
    }

    //根据时间和用户id查询
    public PeachBlossomUser findByUserIdAndDate(Long userId, String today) {
        Criteria criteria = Criteria.where("userId").is(userId).and("date").is(today);
        Query query = Query.query(criteria);
        PeachBlossomUser pu = mongoTemplate.findOne(query, PeachBlossomUser.class);
        return pu;
    }

    //数据库已有数据 更新
    public void update(PeachBlossomUser peachBlossomUser, Integer count) {

        Query query = Query.query(Criteria.where("id").is(peachBlossomUser.getId()));

        Update update = new Update();
        update.set("remainingTimes",count);

        mongoTemplate.updateFirst(query,update, PeachBlossomUser.class);
    }
}
